/*This file is part of WZLibCP.

 *  WZLibCP is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

 *  WZLibCP is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

 *  You should have received a copy of the GNU General Public License
    along with WZLibCP.  If not, see <http://www.gnu.org/licenses/>.
		
	Author: calc0000
*/
#include "stdafx.h"
#include "WZExtendedProperty.h"

#include "WZImage.h"
#include "WZSubProperty.h"
#include "WZCanvasProperty.h"
#include "WZPrimitiveProperties.h"
#include "WZConvexProperty.h"
#include "WZSoundProperty.h"
#include "WZUOLProperty.h"

using namespace std;

namespace WZLib{
	WZImageProperty*	WZExtendedProperty::prop(){return this->_prop;}
	ctor WZExtendedProperty::WZExtendedProperty(WZImage* parent,string name):WZImageProperty(parent,Prop_Extended,name,Property){
		this->_prop=NULL;
	}
	dtor WZExtendedProperty::~WZExtendedProperty(){
		//delete this->_prop;
	}
	ErrorCode WZExtendedProperty::parse(WZLib::WZBinaryReader *reader){
		return Error_WZLib_WZExtendedProperty_Parse_WrongParseFunction;
	}
	ErrorCode WZExtendedProperty::parse(WZLib::WZBinaryReader *reader, unsigned int offset, int eob){
		byte b=reader->ReadByte();
		string iname="";
		if(b==0x1B){
			int readPos=offset+reader->ReadInt32();
			long currentPos=reader->Position();
			reader->Position(readPos);
			iname=reader->ReadEncryptedString();
			reader->Position(currentPos);
		}else
			iname=reader->ReadEncryptedString();
		if(iname=="Property"){
			WZSubProperty* sprop=new WZSubProperty(this->_imgParent,this->_name);
			*(sprop->parent())=this;
			reader->IncPosition(2);
			ErrorCode r=sprop->parse(reader,offset);
			this->_prop=sprop;
		}else if(iname=="Canvas"){
			WZCanvasProperty* cprop=new WZCanvasProperty(this->_imgParent,this->_name);
			reader->IncPosition(1);
			if(reader->ReadByte()==1){
				reader->IncPosition(2);
				cprop->parse(reader,offset);
			}
			*(cprop->png())=new WZPNGProperty(this->_imgParent,this->_name,reader);
			*(cprop->parent())=this;
			this->_prop=cprop;
		}else if(iname=="Shape2D#Vector2D"){
			WZVectorProperty* vprop=new WZVectorProperty(reader->ReadCompressedInt(),reader->ReadCompressedInt(),this->_imgParent,this->_name);
			*(vprop->parent())=this;
			this->_prop=vprop;
		}else if(iname=="Shape2D#Convex2D"){
			WZConvexProperty* cprop=new WZConvexProperty(this->_imgParent,this->_name);
			int ec=reader->ReadCompressedInt();
			for(int i=0;i<ec;i++){
				WZExtendedProperty* ex=new WZExtendedProperty(this->_imgParent,this->_name);
				ex->parse(reader,offset,eob);
				WZImageProperty* s=ex->prop();
				cprop->properties().push_back(s);
				*(s->parent())=cprop;
				delete ex;
			}
			*(cprop->parent())=this;
			this->_prop=cprop;
		}else if(iname=="Sound_DX8"){
			WZSoundProperty* sprop=new WZSoundProperty(this->_imgParent,this->_name);
			sprop->parse(reader);
			*(sprop->parent())=this;
			this->_prop=sprop;
		}else if(iname=="UOL"){
			WZUOLProperty* uprop=new WZUOLProperty(this->_imgParent,this->_name);
			uprop->parse(reader,offset);
			*(uprop->parent())=this;
			this->_prop=uprop;
		}else{
			int i=3;
		}
		this->_parsed=true;
		return Error_NoError;
	}
};